<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <form id="my-form" class="am-form tpl-form-line-form" method="post">
                    <input type="hidden" name="s" value="">
                    <input type="hidden" name="step" value="2">
                    <div class="widget-body">
                        <fieldset>
                            <div class="widget-head am-cf">
                                <div class="widget-title am-fl">添加商品评价</div>
                            </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require"> 商品 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <div class="am-form-file am-margin-top-xs">
                                            <button type="button"
                                                    class="j-selectGoods upload-file am-btn am-btn-secondary am-radius">
                                                <i class="am-icon-cloud-upload"></i> 选择商品
                                            </button>
                                            <div class="widget-goods-list uploader-list am-cf">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require"> 用户 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <label class="am-radio-inline">
                                            <input type="radio" name="comment[user]" value="0" data-am-ucheck checked>
                                            匿名
                                        </label>
                                        <label class="am-radio-inline">
                                            <input type="radio" name="comment[user]" value="1" data-am-ucheck>
                                            用户
                                        </label>
                                        <div class="am-form-file am-margin-top-xs">
                                            <button type="button"
                                                    class="j-selectUsers upload-file am-btn am-btn-secondary am-radius">
                                                <i class="am-icon-cloud-upload"></i> 选择用户
                                            </button>
                                            <div class="widget-users-list uploader-list am-cf">
                                            </div>
                                        </div>
                                        <div class="help-block">
                                            <small>用户时必须指定用户</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require"> 评价内容 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <textarea class="am-field-valid" rows="5" placeholder=""
                                                  name="comment[content]" required></textarea>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require"> 评分 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <label class="am-radio-inline">
                                            <input type="radio" name="comment[score]" value="10" data-am-ucheck checked>
                                            好评
                                        </label>
                                        <label class="am-radio-inline">
                                            <input type="radio" name="comment[score]" value="20" data-am-ucheck>
                                            中评
                                        </label>
                                        <label class="am-radio-inline">
                                            <input type="radio" name="comment[score]" value="30" data-am-ucheck>
                                            差评
                                        </label>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require"> 是否图片评价 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <label class="am-radio-inline">
                                            <input type="radio" name="comment[is_picture]" value="0" data-am-ucheck checked>
                                            否
                                        </label>
                                        <label class="am-radio-inline">
                                            <input type="radio" name="comment[is_picture]" value="1" data-am-ucheck>
                                            是
                                        </label>
                                        <div class="am-form-file">
                                            <div class="am-form-file">
                                                <button type="button"
                                                        class="select-logo upload-file am-btn am-btn-secondary am-radius">
                                                    <i class="am-icon-cloud-upload"></i> 选择图片
                                                </button>
                                                <div class="uploader-list am-cf">
                                                </div>
                                            </div>
                                            <div class="help-block am-margin-top-sm">
                                                <small>尺寸750x750像素以上，大小2M以下 (可拖拽图片调整显示顺序 )</small>
                                            </div>
                                        </div>
                                        <div class="help-block">
                                            <small>图片评价时必须选择图片</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require"> 是否显示 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <label class="am-radio-inline">
                                            <input type="radio" name="comment[status]" value="1" data-am-ucheck checked>
                                            是
                                        </label>
                                        <label class="am-radio-inline">
                                            <input type="radio" name="comment[status]" value="0" data-am-ucheck>
                                            否
                                        </label>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require"> 排序 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <input type="number" class="tpl-form-input" name="comment[sort]" min="0" value="100" required>
                                        <div class="help-block">
                                            <small>数值越小越前</small>
                                        </div>
                                    </div>
                                </div>
                            <div class="am-form-group">
                                <div class="am-u-sm-10 am-u-sm-push-2 am-margin-top-lg">
                                    <button type="submit" class="j-submit am-btn am-btn-sm am-btn-secondary">提交
                                    </button>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 图片文件列表模板 -->
{{include file="layouts/_template/tpl_file_item" /}}

<!-- 文件库弹窗 -->
{{include file="layouts/_template/file_library" /}}

<!-- 用户列表 -->
<script id="tpl-users-list-item" type="text/template">
    {{ each $data }}
    <div class="file-item">
        <a href="{{ $value.avatarUrl }}" title="{{ $value.nickname }}" target="_blank">
            <img src="{{ $value.avatarUrl }}">
        </a>
        <input type="hidden" name="comment[user_id]" value="{{ $value.user_id }}">
    </div>
    {{ /each }}
</script>

<!-- 商品列表 -->
<script id="tpl-goods-list-item" type="text/template">
    {{ each $data }}
    <div class="file-item">
        <a href="{{ $value.image }}" title="{{ $value.goods_name }}" target="_blank">
            <img src="{{ $value.image }}">
        </a>
        <input type="hidden" name="comment[goods_id]" value="{{ $value.goods_id }}">
    </div>
    {{ /each }}
</script>

<script src="assets/common/js/ddsort.js"></script>
<script src="assets/common/plugins/laydate/laydate.js"></script>
<script src="assets/store/js/select.data.js?v=<?= $version ?>"></script>
<script>
    $(function () {

        // 选择图片
        $('.select-logo').selectImages({
            name: 'comment[file_id][]',
            multiple: true
        });

        // 图片列表拖动
        $('.uploader-list').DDSort({
            target: '.file-item',
            delay: 100, // 延时处理，默认为 50 ms，防止手抖点击 A 链接无效
            floatStyle: {
                'border': '1px solid #ccc',
                'background-color': '#fff'
            }
        });

        // 选择用户
        $('.j-selectUsers').selectData({
            title: '选择用户',
            uri: 'user/lists',
            dataIndex: 'user_id',
            done: function (data) {
                data = [data[0]];
                var $html = $(template('tpl-users-list-item', data));
                $('.widget-users-list').html($html);
            }
        });

        // 选择商品
        $('.j-selectGoods').selectData({
            title: '选择商品',
            uri: 'goods/lists',
            dataIndex: 'goods_id',
            done: function (data) {
                data = [data[0]];
                var $html = $(template('tpl-goods-list-item', data));
                $('.widget-goods-list').html($html);
            }
        });


        /**
         * 表单验证提交
         * @type {*}
         */
        $('#my-form').superForm({
            // 自定义验证
            validation: function () {
                //选择商品
                let goods_id = $('input:hidden[name="comment[goods_id]"]').val()
                if (goods_id == undefined) {
                    layer.msg("请选择要评价的商品")
                    return false
                }
                //选择用户
                let user = $('input:radio[name="comment[user]"]:checked').val()
                let user_id = $('input:hidden[name="comment[user_id]"]').val()
                if (user == 1 && user_id == undefined) {
                    layer.msg("用户评价时请选择用户")
                    return false
                }
                let picture = $('input:radio[name="comment[is_picture]"]:checked').val()
                let file_id = $('input:hidden[name="comment[file_id][]"]').val()
                if (picture == 1 && file_id == undefined) {
                    layer.msg("图片评价时请选择图片")
                    return false
                }

                return true;
            }
        });
    });
</script>
